package com.inkFlow.crawl.core.template.text;


import cn.dev33.satoken.context.mock.SaTokenContextMockUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.inkFlow.book.domain.InkBook;
import com.inkFlow.book.domain.InkBookChapter;
import com.inkFlow.book.domain.InkBookToc;
import com.inkFlow.book.mapper.InkBookChapterMapper;
import com.inkFlow.book.mapper.InkBookMapper;
import com.inkFlow.book.mapper.InkBookTocMapper;
import com.inkFlow.common.core.constant.CrawlConstants;
import com.inkFlow.common.core.domain.model.LoginUser;
import com.inkFlow.common.core.utils.SpringUtils;
import com.inkFlow.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.Objects;

/**
 * @Description: 小说通用存储管道
 * @author: dingshuai  Simple
 * @date: 2025-08-14 10:08:09
 **/
@Component
public class SimpleTextPipeline implements Pipeline {

    private final InkBookMapper inkBookMapper;
    private final InkBookChapterMapper inkBookChapterMapper;
    private final InkBookTocMapper inkBookTocMapper;

    public SimpleTextPipeline() {
        this.inkBookMapper = SpringUtils.getBean(InkBookMapper.class);;
        this.inkBookChapterMapper = SpringUtils.getBean(InkBookChapterMapper.class);;
        this.inkBookTocMapper = SpringUtils.getBean(InkBookTocMapper.class);;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        String pipelineLevel = resultItems.get(CrawlConstants.PIPELINE_LEVEL_KEY);
        switch (pipelineLevel) {
            case CrawlConstants.PIPELINE_LEVEL_VAULE_CATEGORY_PAGE:
                this.saveCategorPage(resultItems, task);
                break;
            case CrawlConstants.PIPELINE_LEVEL_VAULE_INFO_PAGE:
                this.saveInfoPage(resultItems, task);
                break;
            case CrawlConstants.PIPELINE_LEVEL_VAULE_CONTENT_PAGE:
                this.saveContentPage(resultItems, task);
                break;
            default:
                break;
        }
    }

    /**
     * 保存小说列表
     * @param resultItems
     * @param task
     */
    private void saveCategorPage(ResultItems resultItems, Task task) {
        // 获取小说name id url
        List<InkBook> categoryBookInfoList = resultItems.get("categoryBookInfoList");
        if (!CollectionUtils.isEmpty(categoryBookInfoList)) {
            inkBookMapper.insertOrUpdateBatch(categoryBookInfoList);
        }

    }
    /**
     * 保存小说详情
     * @param resultItems
     * @param task
     */
    private void saveInfoPage(ResultItems resultItems, Task task) {
        InkBook bookInfo = resultItems.get("bookInfo");
        if (Objects.nonNull(bookInfo)) {
            inkBookMapper.insertOrUpdate(bookInfo);
        }
        List<InkBookChapter> inkBookChapterList = resultItems.get("inkBookChapterList");
        if (!CollectionUtils.isEmpty(inkBookChapterList)) {
            for (InkBookChapter inkBookChapter : inkBookChapterList) {
                inkBookChapterMapper.insertOrUpdate(inkBookChapter);
                InkBook book = inkBookMapper.selectById(inkBookChapter.getBookId());
                if (Objects.nonNull(book)) {
                    // 新增目录
                    InkBookToc newBookToc = new InkBookToc();
                    newBookToc.setBookId(inkBookChapter.getBookId());
                    newBookToc.setChaptherId(inkBookChapter.getChapterId());
                    newBookToc.setBookName(book.getBookName());
                    newBookToc.setChaptherName(inkBookChapter.getChapterName());
                    inkBookTocMapper.insertOrUpdate(newBookToc);
                }
            }
        }

    }

    /**
     * 保存小说正文
     * @param resultItems
     * @param task
     */
    private void saveContentPage(ResultItems resultItems, Task task) {
        InkBookChapter updateBookChapter = resultItems.get("updateBookChapter");
        if (Objects.nonNull(updateBookChapter)) {
            inkBookChapterMapper.insertOrUpdate(updateBookChapter);
        }

    }
}
